/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.estacionamiento.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.estacionamiento.bl.VehiculosBlLocal;
import mx.itschapala.dsw2.estacionamiento.modelo.Vehiculos;

/**
 *
 * @author Lindemann
 */
@ManagedBean
@SessionScoped
public class VehiculosBean implements Serilizable{
    
//---------------EJB'S-------------------------
    @EJB
    private VehiculosBlLocal vehiculosBl;
 //------------------variables(propiedades)--------------
    private List<Vehiculos> lista;
    private Vehiculos cars;
    private String accion;
//-----------constructor--------------------------------
    public VehiculosBean() {
        cars=new Vehiculos();
    }
   //-----------metodos GET y SET--------------------------------

    public List<Vehiculos> getLista() {
        lista=vehiculosBl.getAll();
        return lista;
    }

    public Vehiculos getCars() {
        return cars;
    }

    public void setCars(Vehiculos cars) {
        this.cars = cars;
    }
 //-----------Acciones--------------------------------    
    public String procesarAccion() {
        if(accion.equalsIgnoreCase("Nuevo")){
        vehiculosBl.register(cars);
        
        } else if(accion.equalsIgnoreCase("Editar")){
                vehiculosBl.modify(cars);
            }
        else if(accion.equalsIgnoreCase("Eliminar")){
                vehiculosBl.deleteCars(cars);
}
        accion="";
        return "VehiculosVista";
    }
    //-----------Listeners--------------------------------  
    public void prepararNuevo(ActionEvent evt){//accionEvent de faces
        cars=new Vehiculos();
        accion="Nuevo";
    }
    
    public void prepararEditar(ActionEvent evt){
        System.out.println(evt.getComponent().getClientId());
        String arr[]=evt.getComponent().getClientId().split(":");
        String num=arr[2];
        int index=Integer.parseInt(num);
        System.out.println("el numero es: "+index);
        cars=lista.get(index);
        accion="Editar";
    }
    public void prepararEliminar(ActionEvent evt){
        String arr[]=evt.getComponent().getClientId().split(":");
        String num=arr[2];
        int index=Integer.parseInt(num);
        System.out.println("el numero es: "+index);
        cars=lista.get(index);
        accion="Eliminar";
    }   
    
}